#include "ProfileConfigure.h"
#include <QtDebug>

ProfileConfigure::ProfileConfigure(QWidget * parent):QDialog(parent){
    setupUi(this);
}

void ProfileConfigure::fromOptions(QAbstractButton *button, QString optionName, bool flag){
    if (flag)	button -> setChecked(profile->getOption(optionName));
    else	button -> setChecked( ! profile->getOption(optionName));
}

void ProfileConfigure::toOptions(QAbstractButton *button, QString optionName, bool flag){
    if (flag)	profile->setOption(optionName, button->isChecked());
    else	profile->setOption(optionName, ! button->isChecked());
}

void ProfileConfigure::set(Profile * newProfile){
    DEBUG_F();
    if (newProfile == 0) return;
    profile = newProfile;

    isAlarm->setChecked(profile->getOption("isAlarm"));
    fromOptions(isUseDock, "isUseDock");
    fromOptions(isNotUseDock, "isUseDock", false);
    
    fromOptions(isAlarmAny, "isAlarmAny");
    fromOptions(isAlarmTopic, "isAlarmTopic");
    fromOptions(isAlarmFriend, "isAlarmFriend");
    fromOptions(isAlarmLoveBoard, "isAlarmLoveBoard");
    fromOptions(isAlarmLoveTopic, "isAlarmLoveTopic");

    fromOptions(isAutoCheckNewBoard, "isAutoCheckNewBoard");
    fromOptions(isAutoCheckNewTopic, "isAutoCheckNewTopic");
    fromOptions(isAutoCheckNewArticle, "isAutoCheckNewArticle");
    
    fromOptions(isAutoLogin, "isAutoLogin");
    fromOptions(isGuestMode, "isGuestMode");
    fromOptions(isSavePassword, "isSavePassword");
    fromOptions(isSaveUsername, "isSaveUsername");

    nameEdit	->setText(profile->getName());
    usernameEdit->setText(profile->getUsername());
    passwordEdit->setText(profile->getPassword());

    int type = profile->getType();
    qDebug() << ">> First, Type ="  << type;
    /*
    if (type < 0) type = 0;
    if (type > supportBBS::MaxTypeNumber){
	QMessageBox msg;
	msg.setText(tr("BBS type is error! Auto reset it to default!"));
	msg.exec();
	type = 0;
    }
    */
    bbsType->clear();
    for (int i = 0; i <= supportBBS::MaxTypeNumber;i++){
	bbsType->addItem(supportBBS::SupportTypes[i]);
    }
    bbsType->setCurrentIndex(type);
}

void ProfileConfigure::on_buttonBox_accepted(){
    apply();
    DEBUG_F();
    emit finished(QDialog::Accepted);
    hide();
}

void ProfileConfigure::on_buttonBox_rejected(){
    //close();
    emit finished(QDialog::Rejected);
    hide();
}

void ProfileConfigure::apply(){
    int type = bbsType->currentIndex();
    qDebug() << ">> Then,  Type ="  << type;

    if (type < supportBBS::BYHH) type = supportBBS::BYHH;
    if (type != profile->getType())
	profile->setType(type);
    
    profile->setOption("isAlarm", isAlarm->isChecked());
    toOptions(isUseDock, "isUseDock");
    toOptions(isNotUseDock, "isUseDock", false);

    toOptions(isAlarmAny, "isAlarmAny");
    toOptions(isAlarmTopic, "isAlarmTopic");
    toOptions(isAlarmFriend, "isAlarmFriend");
    toOptions(isAlarmLoveTopic, "isAlarmLoveTopic");
    toOptions(isAlarmLoveBoard, "isAlarmLoveBoard");

    toOptions(isAutoCheckNewBoard, "isAutoCheckNewBoard");
    toOptions(isAutoCheckNewTopic, "isAutoCheckNewTopic");
    toOptions(isAutoCheckNewArticle, "isAutoCheckNewArticle");
    
    toOptions(isAutoLogin, "isAutoLogin");
    toOptions(isGuestMode, "isGuestMode");
    toOptions(isSavePassword, "isSavePassword");
    toOptions(isSaveUsername, "isSaveUsername");
    
    profile->setName(nameEdit->text());
    profile->setUsername(usernameEdit->text());
    profile->setPassword(passwordEdit->text());

}